package cn.rongdao.jrkabao.widget;

import android.app.Activity;
import android.text.TextPaint;
import android.view.View;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.dialog.FRDialog;
import com.mifengkong.frwidget.dialog.FRDialogClickListener;

import java.util.ArrayList;
import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.ui.remind.adapter.DayAdapter;
import cn.rongdao.jrkabao.widget.timeselected.OnWheelChangedListener;
import cn.rongdao.jrkabao.widget.timeselected.OnWheelScrollListener;
import cn.rongdao.jrkabao.widget.timeselected.WheelView;

/**
 * Created by jack on 2017/9/11
 */

public class SelectDateFromBottom {
    private FRDialog frDialog;
    private WheelView scrollerNumberPicker;
    private OnSelectDateListener onSelectDateListener;

    private boolean isRepayment;
    private DayAdapter dayAdapter;
    private int maxTextSize = 18;
    private int minTextSize = 16;
    private Activity activity;

    public SelectDateFromBottom(Activity activity) {
        this.activity = activity;
        frDialog = new FRDialog.CommonBuilder(activity)
                .setContentView(R.layout.dialog_select_date)
                .setFullWidth()
                .setFromBottom()
                .setOnClickListener(R.id.dsd_tv_cancel, new FRDialogClickListener() {
                    @Override
                    public boolean onDialogClick(View view) {
                        return true;
                    }
                })
                .setOnClickListener(R.id.dsd_tv_finish, new FRDialogClickListener() {
                    @Override
                    public boolean onDialogClick(View view) {
                        if (null != onSelectDateListener) {
                            int currentItem = scrollerNumberPicker.getCurrentItem();
                            String dayText = dayAdapter.getItemText(currentItem).toString();
                            onSelectDateListener.getSelectDate(isRepayment, dayText);
                        }
                        return true;
                    }
                })
                .create();
        scrollerNumberPicker = frDialog.getView(R.id.dsd_number_picker);
    }

    public void setRepayment(boolean repayment) {
        isRepayment = repayment;
    }

    public void show() {
        frDialog.show();
    }

    public void dismiss() {
        frDialog.dismiss();
    }

    public void setDataList(List<String> dataList) {
        if (null == dataList || dataList.size() == 0) {
            dataList = getMonthDateList();
        }
        dayAdapter = new DayAdapter(activity, dataList, dataList.size() / 2, maxTextSize, minTextSize);
        scrollerNumberPicker.setViewAdapter(dayAdapter);
        scrollerNumberPicker.setVisibleItems(5);
        scrollerNumberPicker.setCurrentItem(dataList.size() / 2);
        scrollerNumberPicker.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                String currentText = (String) dayAdapter.getItemText(wheel.getCurrentItem());
                setTextviewSize(currentText, dayAdapter, false);
            }
        });

        scrollerNumberPicker.addScrollingListener(new OnWheelScrollListener() {

            @Override
            public void onScrollingStarted(WheelView wheel) {
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                String currentText = (String) dayAdapter.getItemText(wheel.getCurrentItem());
                setTextviewSize(currentText, dayAdapter, false);
            }
        });
    }

    public void setTextviewSize(String curriteItemText, DayAdapter adapter, boolean isNumber) {
        ArrayList<View> arrayList = adapter.getTestViews();
        int size = arrayList.size();
        String currentText;
        for (int i = 0; i < size; i++) {
            TextView textvew = (TextView) arrayList.get(i);
            TextPaint paint = textvew.getPaint();
            currentText = textvew.getText().toString();
            if (curriteItemText.equals(currentText)) {
                if (isNumber) {
                    textvew.setTextSize(maxTextSize);
                } else {
                    textvew.setTextSize(maxTextSize);
                }
                paint.setFakeBoldText(true);
            } else {
                if (isNumber) {
                    textvew.setTextSize(minTextSize);
                } else {
                    textvew.setTextSize(minTextSize);
                }
                paint.setFakeBoldText(false);
            }
        }
    }

    private List<String> getMonthDateList() {
        List<String> list = new ArrayList<>();
        for (int i = 1; i < 32; i++) {
            list.add(FRCollection.appendAsString("每月", FRString.valueOf(i), "日", ""));
        }
        return list;
    }

    public void setOnSelectDateListener(OnSelectDateListener onSelectDateListener) {
        this.onSelectDateListener = onSelectDateListener;
    }

    public interface OnSelectDateListener {
        void getSelectDate(boolean isRepayment, String number);
    }
}